<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form action="xxx" onsubmit="return login()">
        <p>账号：<input id="username"/></p>
        <p>电话：<input id="phone"/></p>
        <p><button>登录</button></p>
    </form>
    <script>
       /* 1.getElementById：通过id属性获得元素节点对象 */
       //案例：当帐号为空时，阻止表单提交
       function login(){
           var name = document.getElementById("username").value;
           //var phone1 = document.getElementById("phone").value;
           if(name == ""){
               alert("账号不能为空！");
               return false;  //阻止表单提交
           }
           return true; //让表单提交
       }
    </script>
</body>
</html>